package com.woniu.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryTrainingDTO;
import com.woniu.entity.dto.QueryTrainingLevelDTO;
import com.woniu.mapper.TrainingMapper;
import com.woniu.pojo.Training;
import com.woniu.pojo.TrainingLevel;
import com.woniu.service.ITrainingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaosong
 * @since 2023-03-31
 */
@RestController
@RequestMapping("/training")
@Api(tags = "靶场表管理")
public class TrainingController extends BaseController {
    @Autowired
    private ITrainingService trainingService;

    @ApiOperation("分页+搜索")
    @PostMapping("/listSearch/{currentPage}/{pageSize}")
    public Result listSearch(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody QueryTrainingDTO queryTrainingDTO) {
        PageData pageData = trainingService.listSearch(currentPage, pageSize, queryTrainingDTO);
        return toDataResult(pageData);
    }

    @ApiOperation("新增或修改")
    @PostMapping("/insertOrUpdate")
    public Result insertOrUpdate(@RequestBody Training training) {
        Boolean b = null;
        if (StringUtils.isEmpty(training.getId())) {
            b = trainingService.save(training);
        } else {
            b = trainingService.updateById(training);
        }
        return toResult(b);
    }

    @ApiOperation("ID删除")
    @PostMapping("/delete/{id}")
    public Result deleteTraining(@PathVariable Integer id) {
        boolean b = trainingService.removeById(id);
        return toResult(b);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Integer[] ids) {
        boolean b = trainingService.removeBatchByIds(Arrays.asList(ids));
        return toResult(b);
    }

    @ApiOperation("根据ID修改")
    @PostMapping("/update")
    public Result updateTraining(@RequestBody Training training) {
        boolean b = trainingService.updateById(training);
        return toResult(b);
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/select/{id}")
    public Result selectById(@PathVariable Integer id) {
        QueryWrapper<Training> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        List<Training> list = trainingService.list(queryWrapper);
        return toDataResult(list);
    }

    @ApiOperation("查询所有")
    @GetMapping("/list")
    public Result listAll() {
        List<Training> list = trainingService.list();
        return toDataResult(list);
    }

    @ApiOperation("标题校验")
    @GetMapping("/nameOnly/{name}")
    public Result nameIsExist(@PathVariable String name){
        Boolean flag = trainingService.nameIsExist(name);
        return toDataResult(flag);
    }


    @ApiOperation("修改VIP状态")
    @GetMapping("/setVip/{id}/{isVip}")
    public Result changeSwitchVIP(@PathVariable Integer id, @PathVariable Integer isVip){
        Boolean flag = trainingService.switchVIP(id,isVip);
        return toDataResult(flag);
    }

    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void exportExcel(@RequestBody(required = false) QueryTrainingDTO queryTrainingDTO,
                            HttpServletResponse response) throws IOException {
        PageData pageData = trainingService.listSearch(1, 1000, queryTrainingDTO);
        List<Training> trainingList = (List<Training>) pageData.getData();
        EasyExcel.write(response.getOutputStream(), Training.class).sheet().doWrite(trainingList);
    }


}
